package com.xiaotu.spider.license.pageprocessor;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.MoviePublicMode;
import com.xiaotu.common.util.*;
import com.xiaotu.spider.AUrlPageProcessor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 电影备案立项公示
 */
@Component("MoviePublicPageProcessor")
public class MoviePublicPageProcessor extends AUrlPageProcessor {
    private String start_url = "http://www.chinafilm.gov.cn/chinafilm/channels/167.shtml";

    private static final String MOVIE_PUBLIC_REDIS_KEY = "MOVIE_PUBLIC_CACHE";

    @Autowired
    private CacheHandler cacheHandler;

    @SuppressWarnings("unchecked")
    @Override
    public void process(Page page) {

        Map<String, Object> extrasMap = page.getRequest().getExtras();

        switch (this.getPageDataType(page)) {

            case Constants.PageDataType.SEARCH_LIST:
                this.searchListProcess(page, extrasMap);
                break;
            case Constants.PageDataType.MOVIE_RELATE:
                this.contentProcess(page, extrasMap);
                break;
        }
    }

    /**
     * 备案公示查询结果
     *
     * @param page
     */
//    private void queryResultProcess(Page page, Map<String, Object> extrasMap) {
//        Html html = page.getHtml();
//        List<String> tdValList = html.xpath("//tbody//tr//td[@align='left']//text()").all();
//        if (tdValList.isEmpty() || tdValList.size() != 8)
//            throw new SpiderException(
//                    SpiderExceptionCode.DataInvalideException);
//        //电影类别
//        String movieType = tdValList.get(0);
//        //备案立项号
//        String projectNo = tdValList.get(1);
//        //片名
//        String movieName = tdValList.get(2);
//        //备案单位
//        String filingUnit = tdValList.get(3);
//        //编剧
//        String screenwriter = tdValList.get(4);
//        //备案结果
//        String recordResults = tdValList.get(5);
//        //备案地
//        String placeFiling = tdValList.get(6);
//        //梗概
//        String outLine = tdValList.get(7);
//
//        MoviePublicMode moviePublicMode = new MoviePublicMode(extrasMap.get("aTitle").toString(),
//                extrasMap.get("sitePubDate").toString(),
//                null, projectNo, movieName, filingUnit,
//                screenwriter, recordResults, placeFiling, outLine);
//        //保存数据
//        page.putField(MoviePublicMode.class.getName(), moviePublicMode);
//    }
    private void searchListProcess(Page page, Map<String, Object> extraMap) {

        Html html = page.getHtml();
        //获取下一页地址
        String nextPageUrl = html.xpath("//div[@class='m2R']//div[@class='page']//a[3]/@href").get();
        if (StringUtils.isNotEmpty(nextPageUrl)) {
            PageUtils.addTargetRequest(page, nextPageUrl, extraMap);
        }

        //获取连接
        List<String> aTagList = html.xpath("//div[@class='m2R']//ul[@class='m2ru1 m2ru11']//li//a/@href").all();
        List<String> aTitleList = html.xpath("//div[@class='m2R']//ul[@class='m2ru1 m2ru11']//li//a/text()").all();
        List<String> dateList = html.xpath("//div[@class='m2R']//ul[@class='m2ru1 m2ru11']//li//span/text()").all();
        int temp = 0;
        for (int i = temp; i < dateList.size(); ) {
            dateList.remove(i);
            temp = i++;
        }
        if (aTagList.isEmpty() || aTagList.size() != dateList.size())
            throw new SpiderException(
                    SpiderExceptionCode.DataInvalideException);

        for (int i = 0; i < aTagList.size(); i++) {
            Map<String, Object> eMap = new HashMap<String, Object>();
            eMap.put("aTitleLink", aTagList.get(i));
            eMap.put("aTitle", aTitleList.get(i));
            eMap.put("sitePubDate", dateList.get(i));
            String key = dateList.get(i) + "_" + aTitleList.get(i);
            //保存致缓存中
            boolean movie_pub_cache = cacheHandler.cacheExist(MOVIE_PUBLIC_REDIS_KEY);
            if (!movie_pub_cache) {
                cacheHandler.hSet(MOVIE_PUBLIC_REDIS_KEY, key, "");
                PageUtils.addTargetRequest(page, aTagList.get(i), eMap);
            } else {
                if (!cacheHandler.hExists(MOVIE_PUBLIC_REDIS_KEY, key)) {
                    cacheHandler.hSet(MOVIE_PUBLIC_REDIS_KEY, key, "");
                    PageUtils.addTargetRequest(page, aTagList.get(i), eMap);
                }
            }
        }
    }


    private void contentProcess(Page page, Map<String, Object> extrasMap) {
        Html html = page.getHtml();
        List<MoviePublicMode> modeList = new ArrayList<MoviePublicMode>();
        List<String> movieTypeList = html.xpath("//div[@class='hmc4Box2']//div[@class='hmc4ChgBtn']//a//text()").all();
        List<String> tableList = html.xpath("//table").all();
        for (int i = 0; i < tableList.size(); i++) {
            Html tableHtml = new Html(tableList.get(i));
            List<String> trList = tableHtml.xpath("//tr").all();
            for (int j = 1; j < trList.size(); j++) {
                Html trHtml = new Html("<table>" + trList.get(j) + "</table>");
                List<String> tdList = trHtml.xpath("//td").all();
                //备案立项号
                String projectNo = new Html("<table>" + tdList.get(1) + "</table>").xpath("//td//a//text()").get().replace(" ", "");
                //片名
                String movieName = new Html("<table>" + tdList.get(2) + "</table>").xpath("//td//text()").get().replace(" ", "");
                //备案单位
                String filingUnit = new Html("<table>" + tdList.get(3).replace("<br>", ",") + "</table>").xpath("//td//text()").get().replace(" ", "");
                //编剧
                String screenwriter = new Html("<table>" + tdList.get(4).replace("<br>", ",") + "</table>").xpath("//td//text()").get().replace(" ", "");
                //备案结果
                String recordResults = new Html("<table>" + tdList.get(5) + "</table>").xpath("//td//text()").get().replace(" ", "");
                //备案地
                String placeFiling = new Html("<table>" + tdList.get(6) + "</table>").xpath("//td//text()").get().replace(" ", "");

                modeList.add(new MoviePublicMode(extrasMap.get("aTitle").toString(),
                        extrasMap.get("sitePubDate").toString(),
                        movieTypeList.get(i), projectNo, movieName, filingUnit,
                        screenwriter, recordResults, placeFiling));
            }
        }

        //保存数据
        page.putField(MoviePublicMode.class.getName(), modeList);

    }


    @Override
    protected String[] getStartRequestUrls() {
        return new String[]{start_url};
    }

    @Override
    protected String getSpiderType() {
        return Constants.SpiderTaskType.MOVIE_PUBLIC;
    }

    @Override
    public String getPageDataType(Page page) {
        String url = page.getRequest().getUrl();
        if (url.contains("/chinafilm/channels"))
            return Constants.PageDataType.SEARCH_LIST;
        else if (url.contains("/chinafilm/contents"))
            return Constants.PageDataType.MOVIE_RELATE;
        return StringUtils.EMPTY;
    }
}
